﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SilverlightCommands;
using System.ServiceModel;
using KMT_Pro.Web;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using KMT_Pro.Views.ClientViews;
using KMT_Pro.Utils;

namespace KMT_Pro.ViewModels.ClientViewModels
{
    public class ClientUserMasterViewModel : MessageSendableViewModel
    {
        #region Constants and Fields

        CustomerContext _context;

        //업체리스트
        private readonly ObservableCollection<SP_ClientUserMaster_Select_Result> ClientList = new ObservableCollection<SP_ClientUserMaster_Select_Result>();
        private PagedCollectionView clientUserCollection;
        private SP_ClientUserMaster_Select_Result selectedClientUserMaster;

        //검색어
        public string keyWord = string.Empty;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 생성자
        /// </summary>
        public ClientUserMasterViewModel()
        {
            //목록 조회
            this.SearchClick = new RelayCommand((p) =>
                                                    {
                                                        try
                                                        {
                                                            _context = new CustomerContext();

                                                            this.ServerRun(_context.Load(_context.GetClientUserMaster_SelectQuery(this.KeyWord))
                                                                , (sender, e) =>
                                                                {
                                                                    //비동기 이벤트 등록
                                                                    try
                                                                    {
                                                                        ClientList.Clear();
                                                                        LoadOperation lo = sender as LoadOperation;
                                                                        foreach (SP_ClientUserMaster_Select_Result GAP in lo.AllEntities)
                                                                        {
                                                                            ClientList.Add(GAP);
                                                                        }
                                                                        ClientUserCollection = new PagedCollectionView(this.ClientList);
                                                                    }
                                                                    catch (Exception)
                                                                    { }
                                                                });
                                                        }
                                                        catch (Exception)
                                                        { }
                                                    });
            //수정
            this.editClick = new RelayCommand((p) =>
                                                {
                                                    try
                                                    {
                                                        ChildWindow ClientEditUserView = new ClientEditUserView(this.SelectedClientUserMaster.CompanyID.Trim()
                                                                                                                , this.SelectedClientUserMaster.CompanyName.Trim()
                                                                                                                , this.SelectedClientUserMaster.Name.Trim()
                                                                                                                , this.SelectedClientUserMaster.Tel.Trim()
                                                                                                                , this.SelectedClientUserMaster.Phone.Trim()
                                                                                                                , this.SelectedClientUserMaster.Fax.Trim()
                                                                                                                , this.SelectedClientUserMaster.Email.Trim()
                                                                                                                , this.SelectedClientUserMaster.Position.Trim()
                                                                                                                , this.SelectedClientUserMaster.etc.Trim()
                                                                                                                , this.SelectedClientUserMaster.idx.ToString().Trim()
                                                                                                                , "Update");
                                                        ClientEditUserView.Closed += new EventHandler(ClientEditUserView_Closed);
                                                        ClientEditUserView.Show();
                                                    }
                                                    catch (Exception)
                                                    { }
                                                });
            //삭제
            this.delClick = new RelayCommand((p) =>
                                                {
                                                    try
                                                    {
                                                        if (MessageBox.Show(this.SelectedClientUserMaster.Name.Trim() + " => 정말 삭제하시겠습니까?", "경고!", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                                                        {
                                                            return;
                                                        }

                                                        _context = new CustomerContext();

                                                        this.ServerRun(_context.Load(_context.ClientUserMaster_DeleteQuery(this.SelectedClientUserMaster.idx.ToString()))
                                                                , (sender, e) =>
                                                                {
                                                                    //비동기 이벤트 등록
                                                                    try
                                                                    {
                                                                        LoadOperation lo = sender as LoadOperation;
                                                                        foreach (ResultMessage GAP in lo.AllEntities)
                                                                        {
                                                                            MessageBox.Show(GAP.ExceptionMessage);
                                                                            this.SearchClick.Execute(null);
                                                                        }
                                                                    }
                                                                    catch (Exception)
                                                                    { }
                                                                });
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        ChildWindow errorWin = new ErrorWindow(ex);
                                                        errorWin.Show();
                                                    }
                                                });
            //신규등록
            this.NewClick = new RelayCommand((p) =>
                                                {
                                                    try
                                                    {
                                                        ChildWindow ClientEditUserView = new ClientEditUserView("", "", "", "", "", "", "", "", "", "", "Insert");
                                                        ClientEditUserView.Closed += new EventHandler(ClientEditUserView_Closed);
                                                        ClientEditUserView.Show();
                                                    }
                                                    catch (Exception)
                                                    { }
                                                });

            //로드시 검색
            this.SearchClick.Execute(null);
        }

        #endregion

        #region Properties

        // ------------------------ 속성 : Start -----------------------------------

        public PagedCollectionView ClientUserCollection
        {
            set
            {
                this.clientUserCollection = value;
                this.OnPropertyChanged("ClientUserCollection");
            }

            get
            {
                return clientUserCollection;
            }

        }

        public string KeyWord
        {
            set
            {
                this.keyWord = value;
                this.OnPropertyChanged("KeyWord");
            }

            get
            {
                return keyWord;
            }

        }

        public SP_ClientUserMaster_Select_Result SelectedClientUserMaster
        {
            set
            {
                this.selectedClientUserMaster = value;
                this.OnPropertyChanged("SelectedClientUserMaster");
            }

            get
            {
                return selectedClientUserMaster;
            }

        }
        // ------------------------ 속성 : End -------------------------------------

        //------------------------- 버튼 : Start -----------------------------------

        public ICommand SearchClick { get; set; }
            
        public ICommand editClick { get; set; }

        public ICommand delClick { get; set; }

        public ICommand NewClick { get; set; }

        //------------------------- 버튼 : End -------------------------------------

        #endregion

        #region Event

        /// <summary>
        /// 수정 팝업 닫기 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ClientEditUserView_Closed(object sender, EventArgs e)
        {
            try
            {
                ClientEditUserView lw = (ClientEditUserView)sender;

                if (lw.DialogResult == true)
                {
                    this.SearchClick.Execute(null);
                }
            }
            catch (Exception)
            { }
        }

        #endregion
    }
}
